package com.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.blog.common.Result;
import com.blog.dto.BlogDto;
import com.blog.mapper.BlogMapper;
import com.blog.pojo.Blog;
import com.blog.pojo.BlogTag;
import com.blog.pojo.Collection;
import com.blog.pojo.Tag;
import com.blog.service.BlogTagService;
import com.blog.service.CollectionService;
import com.blog.service.TagService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 博客收藏
 */
@RestController
@RequestMapping("/collection")
public class CollectionController {

    @Autowired
    private CollectionService collectionService;

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private BlogTagService blogTagService;

    @Autowired
    private TagService tagService;

    /**
     * 收藏博客，取消收藏
     * @param userId 用户id
     * @param blogId 博客id
     * @return Result<String>
     */
    @GetMapping("/addOrCancel")
    public Result<String> addOrCancel(Long userId,Long blogId){
        LambdaQueryWrapper<Collection> collectionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        collectionLambdaQueryWrapper.eq(Collection::getUserId,userId).eq(Collection::getBlogId,blogId);
        Collection collectionServiceOne = collectionService.getOne(collectionLambdaQueryWrapper);
        if (collectionServiceOne == null){//还未收藏该博客
            Collection collection = new Collection();
            collection.setUserId(userId);
            collection.setBlogId(blogId);
            collectionService.save(collection);
            return Result.success("收藏成功！");
        }
        //该博客已在收藏夹中
        collectionService.remove(collectionLambdaQueryWrapper);
        return Result.success("该博客从收藏夹中成功移除！");
    }

    /**
     * 判断是否收藏该博客
     * @param userId 用户id
     * @param blogId 博客id
     * @return Result<Boolean>
     */
    @GetMapping("/isOrNot")
    private Result<Boolean> isOrNot(Long userId,Long blogId){
        LambdaQueryWrapper<Collection> collectionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        collectionLambdaQueryWrapper.eq(Collection::getUserId,userId).eq(Collection::getBlogId,blogId);
        Collection collectionServiceOne = collectionService.getOne(collectionLambdaQueryWrapper);
        if (collectionServiceOne == null)//该用户未收藏该博客
            return Result.success(false);
        else
            return Result.success(true);
    }

    /**
     * 返回用户收藏博客列表（包括博客对应下的所有标签）
     * @param userId 用户id
     * @return Result<List<BlogDto>>
     */
    @GetMapping("/myCollection")
    private Result<List<BlogDto>> myCollection(Long userId){
        LambdaQueryWrapper<Collection> collectionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        collectionLambdaQueryWrapper.eq(Collection::getUserId,userId);
        //查出我所关注的所有博客列表
        List<Collection> collectionList = collectionService.list(collectionLambdaQueryWrapper);
        if (collectionList.size() != 0){//我至少收藏了一个博客
            //遍历列表，获得所有的收藏的博客id
            List<Long> blogIdList = new ArrayList<>();
            for (Collection collection : collectionList) {
                blogIdList.add(collection.getBlogId());
            }
            //根据blogIdList查找所有的博客信息
            List<Blog> blogList = blogMapper.selectBatchIds(blogIdList);
            //还需要找出该博客对应的所有标签，于是要将信息封装到dto内
            List<BlogDto> blogDtoList = blogList.stream().map((item) -> {
                BlogDto blogDto = new BlogDto();
                //1.先拷贝基本信息过去
                BeanUtils.copyProperties(item, blogDto);
                //2.再找出所有标签放入dto
                //2.1先查询出种间关系表的对应的所有数据
                LambdaQueryWrapper<BlogTag> blogTagLambdaQueryWrapper = new LambdaQueryWrapper<>();
                blogTagLambdaQueryWrapper.eq(BlogTag::getBlogId, item.getBlogId());
                List<BlogTag> blogTagList = blogTagService.list(blogTagLambdaQueryWrapper);
                //2.2构建tagList
                List<Tag> tagList = new ArrayList<>();
                for (BlogTag blogTag : blogTagList) {
                    tagList.add(tagService.getById(blogTag.getBlogId()));
                }
                //2.3赋值给dto
                blogDto.setTagList2(tagList);
                return blogDto;
            }).collect(Collectors.toList());
            return Result.success(blogDtoList);
        }
        //未收藏任何博客
        return Result.error("您还没有收藏任何博客！");
    }
}
